#include "testApp.h"

//--------------------------------------------------------------
void testApp::setup(){
    
    vel.set(ofRandom(-1, 1), ofRandom(-1, 1));
    
    imgPos.set(ofRandomWidth()/2, ofRandomHeight()/2);
    
    eliImg.loadImage("eli.jpg");

//    for(int i = 0; i < eliImg.width * eliImg.height * 3; i+=3){
//        eliImg.getPixels()[i + 0] = 255;
//        eliImg.getPixels()[i + 1] = 255;
//        eliImg.getPixels()[i + 2] = 255;
//    }
    
    for(int x = 0; x < eliImg.width; x++){
        for(int y = 0; y < eliImg.height; y++){
            
            int i = (x + y * eliImg.width) * 3;
            
            float r = eliImg.getPixels()[i + 0];
            float g = eliImg.getPixels()[i + 1];
            float b = eliImg.getPixels()[i + 2];
            
            if((r + g + b)/3.0f > 100){
                eliImg.getPixels()[i + 0] = 0;
                eliImg.getPixels()[i + 1] = 0;
                eliImg.getPixels()[i + 2] = 255;
            } else if((r + g + b)/3.0f < 80){
                eliImg.getPixels()[i + 0] = 255;
                eliImg.getPixels()[i + 1] = 0;
                eliImg.getPixels()[i + 2] = 0;
            } else {
                eliImg.getPixels()[i + 0] = 255;
                eliImg.getPixels()[i + 1] = 255;
                eliImg.getPixels()[i + 2] = 255;

            }
        }
    }
    
    eliImg.update();}

//--------------------------------------------------------------
void testApp::update(){

    imgPos += vel;
    
    if(imgPos.x > ofGetWidth() - eliImg.width || imgPos.x < 0){
        vel.x *= -1;
    }
    
    if(imgPos.y > ofGetHeight() - eliImg.height || imgPos.y < 0){
        vel.y *= -1;
    }
    
}

//--------------------------------------------------------------
void testApp::draw(){
    eliImg.draw(0, 0, ofGetWidth(), ofGetHeight());
    
    eliImg.draw(imgPos);
}

//--------------------------------------------------------------
void testApp::keyPressed(int key){

}

//--------------------------------------------------------------
void testApp::keyReleased(int key){

}

//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y ){

}

//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){

}

//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){

}

//--------------------------------------------------------------
void testApp::mouseReleased(int x, int y, int button){

}

//--------------------------------------------------------------
void testApp::windowResized(int w, int h){

}

//--------------------------------------------------------------
void testApp::gotMessage(ofMessage msg){

}

//--------------------------------------------------------------
void testApp::dragEvent(ofDragInfo dragInfo){ 

}